<?php

namespace app\admin\controller;

use app\admin\model\KefuGroup;
use app\admin\model\KefuUser;
use app\admin\model\UserInfo;
use app\common\controller\admin\Base;
use think\Cache;
use think\Db;

class Ledger extends Base
{


    protected $noNeedLogin = ['login'];

    protected $ledgerModel;
    protected $kefuUsermodel;
    protected $userModel;


    public function _initialize()
    {
        parent::_initialize();
        $this->ledgerModel = new \app\admin\model\Ledger();
        $this->kefuUsermodel = new KefuUser();
        $this->userModel = new UserInfo();
    }

    //添加账本记录
    public function addSuLedger()
    {
        if (!$this->is_admin) {
            $this->success("没有权限访问");
        }
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params["name"])) {
                $this->error('请输入成本内容');
            }
            if (empty($params["ledger_amount"])) {
                $this->error('请输入成本金额');
            }
            if (empty($params["ledger_amount_usdt"])) {
                $this->error('请输入成本金额(USDT)');
            }

            $data = [
                'name' => $params["name"],
                'ledger_amount' => $params["ledger_amount"],
                'ledger_amount_usdt' => $params["ledger_amount_usdt"],
            ];

            $ret = \app\admin\model\Ledger::create($data);
            $id = $ret->getLastInsID();
            $data['id'] = $id;
            $this->success("添加成功", "", $data);
        }
    }
    //删除成本
    public function delSuLedger()
    {
        if (!$this->is_admin) {
            $this->success("没有权限访问");
        }
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params["id"])) {
                $this->error('请输入客服组id');
            }


            $where['id'] = $params["id"];
            $this->ledgerModel->update(['is_del' => 1], $where);
            $this->success("删除成功");
        }
    }
    //获取成本列表

    public function getSuLedgerList()
    {

        if (!$this->is_admin) {
            $this->success("没有权限访问");
        }
        $params = $this->request->get();

        $page = isset($params["page"]) ? $params["page"] : 1;
        $pageSize = isset($params["pageSize"]) ? $params["pageSize"] : 10;
        $offset = ($page -1) * $pageSize;
        $result = Db::table('su_ledger')->where('is_del', '=', 0)->page($offset, $pageSize)->order('create_time', 'desc')->select();
        $count = Db::table('su_ledger')->where('is_del', '=', 0)->count('id');
        $data['count'] = $count;
        $data['list'] = $result;
        $this->success("","", $data);

    }
    public function getSuLedgerInfo()
    {
        $params = $this->request->get();
        if (empty($params["id"])) {
            $this->error('请输入成本记录id');

        }
        $id = $params["id"];
        $result = Db::table('su_ledger')->where('id', '=', $id)->find();
        $this->success("","", $result);

    }

    //修改客服组
    public function upSuLedgerInfo()
    {
        if (!$this->is_admin) {
            $this->success("没有权限访问");
        }
        if ($this->request->isPost()) {
            $params = $this->request->post();
            if (empty($params["id"])) {
                $this->error('请输入成本记录id');
            }
            $id = $params["id"];
            $data = [];
            if (!empty($params["name"])) {
                $data['name'] = $params["name"];
            }
            if (!empty($params["ledger_amount"])) {
                $data['ledger_amount'] = $params["ledger_amount"];
            }
            if (!empty($params["ledger_amount_usdt"])) {
                $data['ledger_amount_usdt'] = $params["ledger_amount_usdt"];
            }

            if ($data) {
                $data['id'] = $id;

                $result = Db::table('su_ledger')->update($data);
            }

            $this->success("修改成功");

        }

    }



}
